package org.dromara.run.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.run.domain.bo.RuleBo;
import org.dromara.run.domain.vo.RuleVo;
import org.dromara.run.domain.Rule;
import org.dromara.run.mapper.RuleMapper;
import org.dromara.run.service.IRuleService;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 跑步规则表Service业务层处理
 *
 * @author chl
 * @date 2024-09-03
 */
@RequiredArgsConstructor
@Service
public class RuleServiceImpl implements IRuleService {

    private final RuleMapper baseMapper;

    /**
     * 查询跑步规则表
     */
    @Override
    public RuleVo queryById(Long ruleId){
        return baseMapper.selectVoById(ruleId);
    }

    /**
     * 查询跑步规则表列表
     */
    @Override
    public TableDataInfo<RuleVo> queryPageList(RuleBo bo, PageQuery pageQuery) {
        QueryWrapper<Rule> lqw = buildQueryWrapper(bo);
        Page<RuleVo> result = baseMapper.selectRuleList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询跑步规则表列表
     */
    @Override
    public List<RuleVo> queryList(RuleBo bo) {
        QueryWrapper<Rule> lqw = buildQueryWrapper(bo);
        return baseMapper.selectRuleList(lqw);
    }

    private QueryWrapper<Rule> buildQueryWrapper(RuleBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<Rule> lqw = new QueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(bo.getRuleName()), "prr.rule_name", bo.getRuleName());
        lqw.in(CollectionUtils.isNotEmpty(bo.getIds()), "prr.rule_id", bo.getIds());
        lqw.eq(StringUtils.isNotBlank(bo.getGender()), "prr.gender", bo.getGender());
        lqw.eq(bo.getRuleId()!=null, "prr.rule_id", bo.getRuleId());
//        lqw.eq(bo.getSemesterPeriodBegin() != null, "", bo.getSemesterPeriodBegin());
//        lqw.eq(bo.getSemesterPeriodEnd() != null, "", bo.getSemesterPeriodEnd());
//        lqw.eq(bo.getRuleTimeBegin() != null, "", bo.getRuleTimeBegin());
//        lqw.eq(bo.getRuleTimeEnd() != null, "", bo.getRuleTimeEnd());
//        lqw.eq(bo.getTotalRequired() != null, "", bo.getTotalRequired());
        lqw.eq(StringUtils.isNotBlank(bo.getExtraRunId()), "prr.extra_run_id", bo.getExtraRunId());
        lqw.eq(StringUtils.isNotBlank(bo.getMorningRunId()), "prr.morning_run_id", bo.getMorningRunId());
        return lqw;
    }

    /**
     * 新增跑步规则表
     */
    @Override
    public Boolean insertByBo(RuleBo bo) throws ParseException {
        this.tranData(bo);
        Rule add = MapstructUtils.convert(bo, Rule.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRuleId(add.getRuleId());
        }
        return flag;
    }

    /**
     * 修改跑步规则表
     */
    @Override
    public Boolean updateByBo(RuleBo bo) throws ParseException {
        this.tranData(bo);
        Rule update = MapstructUtils.convert(bo, Rule.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    private void tranData(RuleBo bo) throws ParseException {
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Map<String, Object> params = bo.getParams();
        if(params.get("beginSemesterPeriod")!=null){
            String s = params.get("beginSemesterPeriod").toString();
            bo.setSemesterPeriodBegin(dateFormat.parse(s));
        }
        if(params.get("endSemesterPeriod")!=null){
            String s = params.get("endSemesterPeriod").toString();
            bo.setSemesterPeriodEnd(dateFormat.parse(s));
        }
        if(params.get("beginRuleTime")!=null){
            String s = params.get("beginRuleTime").toString();
            bo.setRuleTimeBegin(dateFormat.parse(s));
        }
        if(params.get("endRuleTime")!=null){
            String s = params.get("endRuleTime").toString();
            bo.setRuleTimeEnd(dateFormat.parse(s));
        }
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Rule entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除跑步规则表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
